﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Collections;
using TreasureBox.Utility;

namespace TreasureBox
{
    public partial class Stock : Form
    {
        private string stockName;
        private string initialPrice;
        private string yesterdayPrice;
        private string currentPrice;
        private string maxPrice;
        private string minPrice;
        public Stock()
        {
            InitializeComponent();
        }
        public void InitStockInfo(string stockCode)
        {
            string[] StockInfo = new string[6];
            WebRequest request = WebRequest.Create("http://hq.sinajs.cn/list=" + stockCode);
            StreamReader str = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.Default);
            string content = str.ReadToEnd();
            int startIndex = content.IndexOf("\"") + 1;
            content = content.Substring(startIndex, content.Length - startIndex);
            string[] target = content.Split(',');
            for (int i = 0; i < 6; i++)
            {
                StockInfo[i] = target[i];
            }
            stockName = StockInfo[0];
            initialPrice = StockInfo[1];
            yesterdayPrice = StockInfo[2];
            currentPrice = StockInfo[3];
            maxPrice = StockInfo[4];
            minPrice = StockInfo[5];
        }
        private void Stock_Load(object sender, EventArgs e)
        {
            Icon icon = new Icon("money.ico");
            this.Icon = icon;
            ArrayList StockList = StockProcess.GetStockList();
            for (int i = 0; i < StockList.Count; i++)
            {
                InitStockInfo(StockList[i].ToString());
                Label lbl = new Label();
                lbl.Text = stockName;
                panel1.Controls.Add(lbl);
            }
            timeStock.Enabled = true;
        }
        public void ShowGraphic(string GraphicType, string stockCode)
        {
            webBrowserGraphic.Navigate(GraphicType + stockCode + ".gif");
        }
        public void DisplayDetails()
        {
            StringBuilder details = new StringBuilder();
            details.Append("现价： " + currentPrice + "\n");
            string change = (Convert.ToDouble(currentPrice) - Convert.ToDouble(yesterdayPrice)).ToString("f2");
            string changepre = (Convert.ToDouble(change) / Convert.ToDouble(yesterdayPrice)).ToString("P", System.Globalization.CultureInfo.InvariantCulture);
            details.Append("涨跌额： " + change + "\n");
            details.Append("涨跌幅： " + changepre + "\n");
            details.Append("名称： " + stockName + "\n");
            details.Append("昨收盘： " + yesterdayPrice + "\n");
            details.Append("今开盘： " + initialPrice + "\n");
            details.Append("最高价： " + maxPrice + "\n");
            details.Append("最低价： " + minPrice + "\n");
            richTextBox1.Text = details.ToString();
        }
        private void timeStock_Tick(object sender, EventArgs e)
        {
            InitStockInfo("sh601328");
            DisplayDetails();
        }
        private void btnShowMinGraphic_Click(object sender, EventArgs e)
        {
            ShowGraphic(StockGraphicUrlList.MinGraphicUrl, "sh601328");
        }
        private void btnDailyKGraphic_Click(object sender, EventArgs e)
        {
            ShowGraphic(StockGraphicUrlList.DailyKGraphicUrl, "sh601328");
        }
        private void btnShowWeeklyKGrahic_Click(object sender, EventArgs e)
        {
            ShowGraphic(StockGraphicUrlList.WeeklyKGrahicUrl, "sh601328");
        }
        private void btnShowMonthlyKGrahic_Click(object sender, EventArgs e)
        {
            ShowGraphic(StockGraphicUrlList.MonthlyKGrahicUrl, "sh601328");
        }
    }
    public static class StockGraphicUrlList
    {
        public static string DailyKGraphicUrl = "http://image2.sinajs.cn/newchart/daily/n/";
        public static string MinGraphicUrl = "http://image2.sinajs.cn/newchart/min/n/";
        public static string WeeklyKGrahicUrl = "http://image2.sinajs.cn/newchart/weekly/n/";
        public static string MonthlyKGrahicUrl = "http://image2.sinajs.cn/newchart/monthly/n/";
    }

}
